﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BranchManger.aspx.cs" Inherits="XYTXL.View.BranchManger" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="/View/CSS/bootstrap3.3.5/css/bootstrap.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="/View/JS/zTreeStyle/zTreeStyle.css" />
    <link rel="stylesheet" type="text/css" href="/View/JS/smartMenu/css/smartMenu.css" />
    <script src="/View/JS/jquery-1.11.2.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/View/JS/zTreeStyle/jquery-ztree-2.5.min.js"></script>
    <script type="text/javascript" src="/View/JS/smartMenu/js/jqueryMenu.js"></script>
    <script type="text/javascript">
        var zTree1, rMenu, setting;
        function getMenu() {
            setting = {
                isSimpleData: true,
                treeNodeKey: "id",
                treeNodeParentKey: "pId",
                showLine: false,

                callback: {
                    rightClick: zTreeOnRightClick,
                    click: zTreeOnClick

                },
                root: {
                    isRoot: true,
                    nodes: []
                }
            };
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "GetBranch" },
                beforeSend: function (XMLHttpRequest) {
                    parent.MyDialog.Mytips();
                },
                success: function (msg) {
                    zNodes = eval(msg);
                    zTree1 = $("#tree").zTree(setting, zNodes);
                    zTree1.expandAll(true);

                },
                complete: function (XMLHttpRequest, textStatus) {
                    parent.MyDialog.CloseMytips();
                },
                error: function () {
                    alert('error');
                }
            });
        }
        function showRMenu(type, x, y) {
            $("#smartMenu_image2").show();
            $("#smartMenu_image2").css({ "top": y + "px", "left": x + "px", "display": "block" });
        }
        function hideRMenu() {
            $("#smartMenu_image2").hide();
        }

        function zTreeOnRightClick(event, treeId, treeNode) {
            if (!treeNode) {
                zTree1.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else if (treeNode && !treeNode.noR) {
                zTree1.selectNode(treeNode);
                showRMenu("bran", event.clientX, event.clientY);
            }

            //筛选数据
            $(".ftitle").text(treeNode.name + '人员');
            $("#MulTab tbody tr").each(function () {
                if ($.trim($(this).find('td').eq(2).text()) != treeNode.name && treeNode.id != '0') {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            })
        }
        function zTreeOnClick(event, treeId, treeNode) {
            //筛选数据
            $(".ftitle").text(treeNode.name + '人员');
            $("#MulTab tbody tr").each(function () {
                if ($.trim($(this).find('td').eq(2).text()) != treeNode.name && treeNode.id != '0') {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            })
        }
        function ManageBranch(action) {

            var node = zTree1.getSelectedNode();
            if (action == 'add') {
                var pageurl = "/View/Base/MangerBranch.aspx?Action=add&Pid=" + node.id;
                parent.MyDialog.viewform(pageurl, '增加部门', 550, 450);
            }
            if (action == 'update') {
                var pageurl = "/View/Base/MangerBranch.aspx?Action=update&Pid=" + node.id;
                parent.MyDialog.viewform(pageurl, '修改部门', 550, 450);
            }
            if (action == 'del') {
                if (node) {
                    if (node.nodes && node.nodes.length > 0) {
                        parent.MyDialog.warning('要删除的节点是父节点，请先删除子节点！');
                    } else {
                        parent.MyDialog.confirm('你确定要删除部门吗？', function () {
                            delBranch(node);
                        });
                    }
                }
                hideRMenu();
            }
            if (action == 'addUser') {
                AddUser();
            }
        }

        //删除部门
        function delBranch(node) {
            $.ajax({
                type: "GET",
                url: "AdminHandle.ashx",
                dataType: "html",
                data: { "Action": "delBranch", "delCode": node.id },
                beforeSend: function (XMLHttpRequest) {
                    parent.MyDialog.Mytips();
                },
                success: function (msg) {
                    if (msg == "success") {
                        zTree1.removeNode(node);
                        parent.MyDialog.CloseMytips();

                        parent.MyDialog.success('删除成功')

                    } else {
                        parent.MyDialog.warning(msg)

                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                },
                error: function () {
                }
            });
        }
        //添加用户
        function AddUser() {
            var node = zTree1.getSelectedNode();
            var pid = node == null ? "1" : node.id;
            var pageurl = "/View/Base/MangerUser.aspx?Action=add&Pid=" + pid;
            parent.MyDialog.viewform(pageurl, '添加用户', 750, 550);

        }
        //添加部门
        function AddBranch() {
            var node = zTree1.getSelectedNode();
            var pid = node == null ? "1" : node.id;
            var pageurl = "/View/Base/MangerBranch.aspx?Action=add&Pid=" + pid;
            parent.MyDialog.viewform(pageurl, '添加部门', 750, 550);

        }

        $(document).ready(function () {
            getMenu();
            rMenu = $("#smartMenu_image2");
            //鼠标不在菜单中按下时隐藏菜单
            $("body").bind("mousedown",
            function (event) {
                if (!(event.target.id == "smartMenu_image2" || $(event.target).parents("#smartMenu_image2").length > 0)) {
                    $("#smartMenu_image2").hide();
                }
            });
            ;
         
            $(".xgyh").click(function () {
                var username = $(this).attr('username');
                var pageurl = "/View/Base/MangerUser.aspx?Action=Update&UserName=" + username;
                parent.MyDialog.viewform(pageurl, '修改用户', 750, 550);
            })

            $(".delyh").click(function () {
                var username = $(this).attr('username');
                var tr = $(this).parent().parent();
                parent.MyDialog.confirm('你确定要删除此用户吗？', function () {

                    $.ajax({
                        type: "GET",
                        url: "AdminHandle.ashx",
                        dataType: "html",
                        data: { "Action": "delUser", "delCode": username },
                        beforeSend: function (XMLHttpRequest) {
                            parent.MyDialog.Mytips();

                        },
                        success: function (msg) {
                            if (true) {
                                tr.remove();
                            }
                        },
                        complete: function (XMLHttpRequest, textStatus) {
                            parent.MyDialog.CloseMytips();

                        },
                        error: function () {
                        }
                    });
                });
            })
            $("#conQuery").keyup(function (e) {
                var text = $(this).val();
                if (text != "") {
                    $("#MulTab tbody tr").each(function () {
                        if ($.trim($(this).find('td').eq(1).text()).indexOf(text) >= 0) {
                            $(this).show();
                        }
                        else {
                            $(this).hide();
                        }
                    })
                }
                else {
                    $("#MulTab tbody tr").show();
                }
            });

        })
        function ImportUser() {
            $("#upFile").trigger("click");
        }
        function YLWJ() {
            if (document.getElementById("upFile").files.length > 0) {
                var formData = new FormData();
                formData.append("upFile", document.getElementById("upFile").files[0]);
                $.ajax({
                    url: "AdminHandle.ashx?ACTION=ImportUser&r=" + Math.random(),
                    type: "POST",
                    data: formData,
                    /**
                    *必须false才会自动加上正确的Content-Type
                    */
                    contentType: false,
                    /**
                    * 必须false才会避开jQuery对 formdata 的默认处理
                    * XMLHttpRequest会对 formdata 进行正确的处理
                    */
                    processData: false,
                    success: function (data) {
                        if (data == "success") {
                            parent.MyDialog.success('导入成功');
                            var piframe = parent.$('iframe:visible')[0];
                            piframe.contentWindow.location.reload();
                        }
                        else if (data == "error") {
                            parent.MyDialog.warning('导入失败');
                        }
                        else {
                            parent.MyDialog.warning(data);
                        }
                    }
                });
            }
            else {
                parent.MyDialog.warning('请上传文件');
            }
        }
    </script>
</head>
<body style="margin: 0">
    <form id="form" runat="server">
        <table id="container">
            <tr>
                <td valign="top">
                    <div id="sidebar" style="margin: 20px;">
                        <ul id="tree" class="tree" style="width:220px;max-height:550px;">
                        </ul>
                    </div>
                </td>
                <td valign="top" style="width: 90%;">
                    <div class="well" style="margin: 20px;">
                        <div>

                            <table class="table">
                                <tr>
                                    <td style="text-align: left">
                                        <asp:Button ID="btAddBranch" CssClass="btn btn-info" runat="server" Text="添加部门" OnClientClick="AddBranch();return false" />

                                        <asp:Button ID="btAddUser" CssClass="btn btn-info" runat="server" Text="添加用户" OnClientClick="AddUser();return false" />

                                        <input type="file" name="upFile" id="upFile" class="form-control" onchange="YLWJ()" accept=".xls,.xlsx" style="display:none;"/>
                                        <button type="button" class="btn btn-info" onclick="ImportUser()">导入用户</button>
                                        <a href="用户模板.xls">下载模板</a>
                                    </td>

                                </tr>
                            </table>
                        </div>
                        <div id="prolist" style="height:500px; overflow:auto">
                            <asp:Repeater ID="RepRepeaterData" runat="server">
                                <HeaderTemplate>
                                    <table id="MulTab" class="table table-bordered table-condensed table-hover"> 
                                        <thead>
                                            <tr>

                                                <th>用户名称
                                                </th>
                                                <th>登录账号
                                                </th>
                                                <th>所在部门
                                                </th>
                                                <th>担任职务
                                                </th>

                                                <th>手机号
                                                </th>
                                                <th>是否启用
                                                </th>
                                                <th>操作
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr id="row">
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "UserRealName")%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "UserName")%>
                                        </td>
                                        <td>
                                            <%#GetBranchName(Convert.ToString(DataBinder.Eval(Container.DataItem, "BranchCode")))%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "zhiwu")%>
                                        </td>

                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "mobphone")%>
                                        </td>
                                        <td>
                                            <%#DataBinder.Eval(Container.DataItem, "IsUse")%>
                                        </td>
                                        <td>
                                            <a username='<%#Eval("UserName") %>' href="#" class="xgyh">修改</a>
                                            &nbsp;
                                            <a  username='<%#Eval("UserName") %>' href="#" class="delyh" style="margin-left:5px;color:red">删除</a>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </tbody> </table >
                                </FooterTemplate>
                            </asp:Repeater>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        <div id="smartMenu_image2" class="smart_menu_box" style="display: none;">
            <div class="smart_menu_body">
                <ul class="smart_menu_ul">
                    <li class="smart_menu_li" onclick="ManageBranch('add')"><a href="javascript:" class="smart_menu_a">增加部门</a></li>
                    <li class="smart_menu_li" onclick="ManageBranch('update')"><a href="javascript:"
                        class="smart_menu_a">修改部门</a></li>
                    <li class="smart_menu_li" onclick="ManageBranch('del')"><a href="javascript:" class="smart_menu_a">删除部门</a></li>
                    <li class="smart_menu_li_separate">&nbsp;</li>
                    <li class="smart_menu_li" onclick="ManageBranch('addUser')"><a href="javascript:"
                        class="smart_menu_a">添加用户</a></li>
                </ul>
            </div>
        </div>
    </form>
</body>
</html>
